# -*- coding: utf-8 -*-
# Create time: 2020-03-11 23:50
# Author:Rongzhikai
import logging
from logging.handlers import RotatingFileHandler
from conf.settings import LOG_FILE


class Logger(object):
    __mylog = None

    def __new__(cls, *args, **kwargs):
        if cls.__mylog is None:
            cls.__mylog = object.__new__(cls)
            return cls.__mylog
        else:
            return cls.__mylog

    def __init__(self):
        self.formater = logging.Formatter('[%(asctime)s] [%(levelname)s] [%(pathname)s : %(funcName)s:%(lineno)d , %(message)s')
        self.logger = logging.getLogger('log')  # 取得日志对象
        self.logger.setLevel(logging.DEBUG)  # 设置日志输出级别

        # 设置日志输出到txt文件
        self.filelogger = RotatingFileHandler(LOG_FILE, encoding='utf-8', maxBytes=5242880, backupCount=3)
        # 设置同步输出到控制台
        self.console = logging.StreamHandler()
        self.console.setLevel(logging.DEBUG)

        # 设置输出字符串格式
        self.filelogger.setFormatter(self.formater)
        self.console.setFormatter(self.formater)

        # 添加两个handler，文件和控制台同步输出
        self.logger.addHandler(self.filelogger)
        self.logger.addHandler(self.console)


myLogger = Logger().logger
# if __name__ == '__main__':
#     myLogger.debug('111111')
#     myLogger.debug('22222')
#     myLogger.debug('33333')
#     myLogger.debug('44444')
